package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class BibliotecaApp {

    ArrayList<Book> bookList = new ArrayList<Book>();
    ArrayList<Movie> movieList = new ArrayList<Movie>();
    boolean running = true;

    public BibliotecaApp(){

        bookList.add(new Book("TDD by example", "Kent", 2010));
        bookList.add(new Book("TDD by example", "Kent", 2011));
        movieList.add(new Movie("Harry Potter", 2011, "David Yates", 8.1));
    }

    public boolean isRunning(){

        return running;
    }

    public String welcomeMessage() {

        return "Welcome to Biblioteca Library!";
    }

    public String quitMessage() {

        return "Thank you!";
    }

    public List<Book> mainMenuListBooks() {

        return bookList;
    }

    public List<Movie> mainMenuListMovies() {

        return movieList;
    }

    public String getOption(int option) {

        switch(option){

            case 1:
                String list = "";
                for(Book book : bookList){

                    list += book.toString();
                    list += "\n";
                }
                return list;
            case 2:
                int isbn = 0;
                System.out.println("Enter the book isbn");
                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                try {
                    isbn = Integer.valueOf(reader.readLine());
                }
                catch(Exception e){}
                return checkoutBook(isbn);
            case 3:
                isbn = 0;
                System.out.println("Enter the book isbn");
                reader = new BufferedReader(new InputStreamReader(System.in));
                try {
                    isbn = Integer.valueOf(reader.readLine());
                }
                catch(Exception e){}
                return returnBook(isbn);
            case 4:
                running = false;
                return quitMessage();

        }
        return "Invalid option";
    }

    public String checkoutBook(int isbn) {

        Book toCheckout;
        try{
            toCheckout = bookList.get(isbn - 1);
        }
        catch(Exception exception){

            return "Sorry! Book Unavailable!";
        }
        if(toCheckout.getIssueStatus())
            return "Sorry! Book Unavailable!";
        toCheckout.setIssued();
        return "Thank you! Enjoy the book!";

    }

    public String returnBook(int isbn) {

        Book toReturn;
        try {

            toReturn = bookList.get(isbn - 1);

        }
        catch(Exception exception){

            return "Sorry! Cannot return!";
        }
        if(!toReturn.getIssueStatus())
            return "Sorry! Cannot return!";
        toReturn.setNotIssued();
        return "Thank you!";
    }

    public String checkoutMovie(int isbn, String user) {

        Movie toCheckout;
        try {

            toCheckout = movieList.get(isbn - 1);
        }
        catch(Exception e){

            return "Sorry!";
        }
        if(toCheckout.getIssueStatus())
            return "Sorry!";
        toCheckout.setIssued();
        toCheckout.setCheckedoutUser(user);
        return "Thank you!";
    }



    public static void main(String[] args) throws Exception{


        BibliotecaApp application = new BibliotecaApp();
        System.out.println("Username:");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String username = reader.readLine();
        System.out.println("Password:");
        String password = reader.readLine();
        if(username.equals("2014-213") && password.equals("passwd")){
            System.out.println("Name: Sharan\nEmail: biblio@email.com\nPhone: 9486666033");
            while(application.isRunning()){

                System.out.println("1. Show book list\n2. Checkout\n3. Return\n4. Quit");
                reader = new BufferedReader(new InputStreamReader(System.in));
                String response = application.getOption(Integer.valueOf(reader.readLine()));
                System.out.println(response);
            }
        }
        System.out.println("Bye!");

    }
}
